package com.springboot.sba.D9_DynamicTask.controller;

import com.alibaba.fastjson.JSONObject;
import com.springboot.sba.D9_DynamicTask.bean.ScheduledVo;
import com.springboot.sba.D9_DynamicTask.config.ScheduledTask1;
import com.springboot.sba.D9_DynamicTask.config.ScheduledTask2;
import com.springboot.sba.D9_DynamicTask.dao.ScheduledMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description:
 * @author: ZZZ
 * @time: 2021/8/8 16:13
 */
@RestController
public class DynamicTackController {
    @Autowired
    private ScheduledTask1 dynamicTask;
    @Autowired
    private ScheduledTask2 scheduledTask2;
    @Autowired
    private ScheduledMapper scheduledMapper;


    @RequestMapping("/changeCorn")
    public String startCron10(@RequestParam("taskKey") String taskKey,@RequestParam("cron") String cron) {
        System.out.println("change");
        dynamicTask.changeCron(taskKey,cron);
        System.out.println("change success");
        return "change success";
    }

    @RequestMapping("/cronstop")
    public String  cronStop(@RequestParam("taskKey") String taskKey) {
        dynamicTask.stopCron(taskKey);
        return "stop success";
    }

    @RequestMapping("/stop")
    public String  stopTask(@RequestBody ScheduledVo scheduled) {
        scheduled.setStatus(0);
        Boolean stop = scheduledTask2.stop(scheduled.getTaskKey());
        if(stop){
            scheduledMapper.edit(scheduled);
        }
        return JSONObject.toJSONString(scheduled);
    }

    @RequestMapping("/restart")
    public String  restartTask(@RequestBody ScheduledVo scheduled) {
        scheduled.setStatus(1);
        Boolean stop = scheduledTask2.restart(scheduled.getTaskKey(),null);
        if(stop){
            scheduledMapper.edit(scheduled);
        }
        return JSONObject.toJSONString(scheduled);
    }

    @RequestMapping("/edit")
    public String  editTask(@RequestBody ScheduledVo scheduled) {

        return JSONObject.toJSONString(scheduled);
    }







}
